package regular.com.baidu;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author fanhongxing
 * @desc 测试正则工具
 */
public class PatternUtil {

	private static void matches() {
		boolean b = Pattern.matches("^.*?\\d+.*?$", "iia88p");
		System.out.println(b);
	}

	public static void split() {
		String input = "aab:op:p";
		Pattern p = Pattern.compile(":");
		String[] splits = p.split(input);
		for (String str : splits)
			System.out.println(str);
	}

	public static void main(String[] args) throws IOException {

		matches();

		split();
		
//		System.exit(0);

		BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(System.in)));
		while (true) {
			System.out.print("Enter your regex: ");
			Pattern pattern = Pattern.compile(br.readLine());
			System.out.print("Enter input string to search: ");
			Matcher matcher = pattern.matcher(br.readLine());
			boolean found = false;
			while (matcher.find()) {
				for (int i = 0; i <= matcher.groupCount(); i++) {
					System.out.println("group(" + i + ")=" + matcher.group(i));
				}
				found = true;
			}
			if (!found) {
				System.out.println("No match found.");
			}
		}
	}
}
